package com.cgtong.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import android.os.Build;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.util.PostUtil;
import com.kingja.loadsir.callback.SuccessCallback;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;

/**
 * Created by Sky.Bing on 2017/10/24.
 */

public class NewsInfoActivity extends BaseTitleActivity {
    public static final String EXTRA_ACTIVITY_ID = "activity_id";

    @BindView(R.id.wv_news_info)
    public WebView mNewsInfoWebView;

    @Override
    protected String getContentTitle() {
        return getResources().getString(R.string.txt_title_newsdynamic);
    }

    /**
     * 启动资讯详情页
     *
     * @param context
     * @param activityId 资讯ID
     */
    public static void actionStart(Activity context, int activityId) {
        Intent intent = new Intent(context, NewsInfoActivity.class);
        intent.putExtra(EXTRA_ACTIVITY_ID, activityId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_info;
    }

    @Override
    protected void loadDataByNet() {
        Intent intent = getIntent();

        WebSettings wSet = mNewsInfoWebView.getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        wSet.setDefaultTextEncodingName("UTF-8");
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
                wSet.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            mNewsInfoWebView.removeJavascriptInterface("searchBoxJavaBridge_");
            mNewsInfoWebView.removeJavascriptInterface("accessibility");
            mNewsInfoWebView.removeJavascriptInterface("accessibilityTraversal");
        } catch (Throwable tr) {
            tr.printStackTrace();
        }
        int activityId = intent.getIntExtra(EXTRA_ACTIVITY_ID, 0);
        String accessToken = App.getInstance().getUserToken();
        //加载资讯详情
        String xqNewsUrl = ApiManager.BASE_URL + ApiManager.URL_GET_NEWSINFO + "?id=" + activityId
                + "&accessToken=" + (StringUtils.isEmpty(accessToken) ? "" : accessToken);
        mNewsInfoWebView.loadUrl(xqNewsUrl);
        LogUtils.i("xqNewsUrl:" + xqNewsUrl);
        mNewsInfoWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
            }
            @Override
            public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        mNewsInfoWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
                }
            }
        });
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }
}